package com.htjc.integral.conversion.tokenUtil;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.htjc.integral.conversion.conf.RedisTem;
import com.htjc.integral.conversion.tokenUtil.service.IUserService;

@Service
public class TokenUtil
{
	private final static Log log = LogFactory.getLog(TokenUtil.class);
//	@Autowired
//	private RemoteServiceProxy remoteServiceProxy;
	
	@Autowired
	private RedisTem redisTem;
	
	
	@Autowired
	private IUserService userService;
	
	public final static String TENANT="state_grid";
	public final static String MEMBER="esgcc_personal";
	
	public TokenUtil()
	{
		result = this;
	}

 	private static TokenUtil result=null;
 	
	public static TokenUtil getInstance(RedisTem redisTem)
	{
		if(result == null)
		{
			result = new TokenUtil();
			return result;
		}
		
		return result;
	}
	
	private String getCacheTokenKey(String token) {
		return "user_token_" + token;
	}
	
	//根据token获取用户信息
	private String getUserInfoByToken(String token) {
		String cackeKey = getCacheTokenKey(token);
		String uscInfos = redisTem.get(cackeKey);
		//如果缓存里有直接获取
		if( uscInfos!=null && !uscInfos.trim().equals("")) {
			log.info("从缓储中获取用户：" + uscInfos);
			return uscInfos;
		}
		JSONObject uscInfo = new JSONObject();
		uscInfo.put("tenant", TENANT);
		uscInfo.put("member", MEMBER);
		//如果缓存中没有去根据token请求
		uscInfos = userService.makeTokenUserId(uscInfo.toString(), token);
		if(StringUtils.equals("0000", JSON.parseObject(uscInfos).getJSONObject("srvrt").getString("resultCode"))){
			log.info("设置用户到缓存：" + uscInfos);
			redisTem.setex(cackeKey, 600, uscInfos);;//缓存10分钟有效
		}
		log.info("获取用户信息返回为:"+uscInfos);
		return uscInfos;
	}

	//111
	public String getUserIdByToken(String token){
		log.info("进入tokenUtil获取token中的参数。。。");
		String resultJson = getUserInfoByToken(token);
         if(StringUtils.equals("0000", JSON.parseObject(resultJson).getJSONObject("srvrt").getString("resultCode")))
         {
        	 String userid =JSON.parseObject(resultJson).getJSONObject("bizrt").getJSONObject("userInfo").getString("userId");
        	 return userid;
         }
         return "";
	}
	
//	public String getUserMobileByToken(String token)
//	{
//		String resultJson = getUserInfoByToken(token);
//         if(StringUtils.equals("0000", JSON.parseObject(resultJson).getJSONObject("srvrt").getString("resultCode")))
//         {
//        	 String mobile =JSON.parseObject(resultJson).getJSONObject("bizrt").getJSONObject("userInfo").getString("mobile");
//        	 return mobile;
//         }
//         return "";
//	}
//	
//	public String getUserNameByToken(String token)
//	{
//		String resultJson = getUserInfoByToken(token);
//         if(StringUtils.equals("0000", JSON.parseObject(resultJson).getJSONObject("srvrt").getString("resultCode")))
//         {
//        	 String loginAccount =JSON.parseObject(resultJson).getJSONObject("bizrt").getJSONObject("userInfo").getString("loginAccount");
//        	 return loginAccount;
//         }
//         return "";
//	}
//	
//	public void saveUserIdByToken(String token, String userId) {
//	}
//	
//	public void delTokenKey(String token)
//	{
//		JSONObject uscInfo = new JSONObject();
//		uscInfo.put("tenant", TENANT);
//		uscInfo.put("member", MEMBER);
//		String userid= getUserIdByToken(token);
//    	 
//		JSONObject quInfo = new JSONObject();
//		quInfo.put("userId", userid);
//		quInfo.put("token", token);
//		
//		log.info("退出登录参数quInfo=:"+quInfo.toString());
//		
//		String	resultJson = userService.logOut(uscInfo.toString(), quInfo.toString());
//		log.info("退出登录返回数据:"+resultJson);
//		
//		String cackeKey = getCacheTokenKey(token);
//		spyMemcachedClient.safeDelete(cackeKey);
//		
//		if (StringUtils.equals("0000", JSON.parseObject(resultJson)
//				.getJSONObject("srvrt").getString("resultCode"))) {
//			log.info(JSON.parseObject(resultJson)
//					.getJSONObject("srvrt").getString("resultMessage"));
//		} else {
//			log.info(resultJson);
//		}
//
//	}
	
}
